.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1989 AT&T
.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
.\" Copyright (c) 2004, Sun Microsystems, Inc.  All Rights Reserved.
.\"
.TH MAKECONTEXT 3C "February 17, 2023"
.SH NAME
makecontext, swapcontext \- manipulate user contexts
.SH SYNOPSIS
.nf
#include <ucontext.h>

\fBvoid\fR \fBmakecontext\fR(\fBucontext_t *\fR\fIucp\fR, \fBvoid (*\fR\fIfunc\fR)(), \fBint\fR \fIargc\fR...);
.fi

.LP
.nf
\fBint\fR \fBswapcontext\fR(\fBucontext_t *restrict\fR \fIoucp\fR,
     \fBconst ucontext_t *restrict\fR \fIucp\fR);
.fi

.SH DESCRIPTION
The \fBmakecontext()\fR function modifies the context specified by \fIucp\fR,
which has been initialized using \fBgetcontext\fR(2). When this context is
resumed using \fBswapcontext()\fR or \fBsetcontext\fR(2), execution continues
by calling the function \fIfunc\fR, passing it the arguments that follow
\fIargc\fR in the \fBmakecontext()\fR call. The value of \fIargc\fR must match
the number of pointer-sized integer arguments passed to \fIfunc\fR, otherwise
the behavior is undefined.
.sp
.LP
Before a call is made to \fBmakecontext()\fR, the context being modified should
have a stack allocated for it. The stack is assigned to the context by
initializing the \fBuc_stack\fR member.
.sp
.LP
The \fBuc_link\fR member is used to determine the context that will be resumed
when the context being modified by \fBmakecontext()\fR returns.  The
\fBuc_link\fR member should be initialized prior to the call to
\fBmakecontext()\fR. If the \fBuc_link\fR member is initialized to \fINULL\fR,
the thread executing \fIfunc\fR will exit when \fIfunc\fR returns. See
\fBpthread_exit\fR(3C).
.sp
.LP
The \fBswapcontext()\fR function saves the current context in the context
structure pointed to by \fIoucp\fR and sets the context to the context
structure pointed to by \fIucp\fR.
.sp
.LP
If the \fIucp\fR or \fIoucp\fR argument points to an invalid address, the
behavior is undefined and \fBerrno\fR may be set to \fBEFAULT\fR.
.SH RETURN VALUES
On successful completion, \fBswapcontext()\fR returns \fB0\fR. Otherwise,
\fB\(mi1\fR is returned and \fBerrno\fR is set to indicate the error.
.SH ERRORS
The \fBswapcontext()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBENOMEM\fR\fR
.ad
.RS 10n
The \fIucp\fR argument does not have enough stack left to complete the
operation.
.RE

.sp
.LP
The \fBswapcontext()\fR function may fail if:
.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 10n
The \fIucp\fR or \fIoucp\fR argument points to an invalid address.
.RE

.SH EXAMPLES
\fBExample 1 \fRAlternate execution context on a stack whose memory was
allocated using \fBmmap()\fR.
.sp
.in +2
.nf
#include <stdio.h>
#include <ucontext.h>
#include <sys/mman.h>

void
assign(long a, int *b)
{
        *b = (int)a;
}

int
main(int argc, char **argv)
{
        ucontext_t uc, back;
        size_t sz = 0x10000;
        int value = 0;

        getcontext(&uc);

        uc.uc_stack.ss_sp = mmap(0, sz,
            PROT_READ | PROT_WRITE | PROT_EXEC,
            MAP_PRIVATE | MAP_ANON, -1, 0);
        uc.uc_stack.ss_size = sz;
        uc.uc_stack.ss_flags = 0;

        uc.uc_link = &back;

        makecontext(&uc, assign, 2, 100L, &value);
        swapcontext(&back, &uc);

        printf("done %d\en", value);

        return (0);
}
.fi
.in -2

.SH USAGE
These functions are useful for implementing user-level context switching
between multiple threads of control within a process (co-processing). More
effective multiple threads of control can be obtained by using native support
for multithreading. See \fBthreads\fR(7).
.SH ATTRIBUTES
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Standard
_
MT-Level	MT-Safe
.TE

.SH SEE ALSO
.BR getcontext (2),
.BR mmap (2),
.BR sigaction (2),
.BR sigprocmask (2),
.BR pthread_exit (3C),
.BR ucontext.h (3HEAD),
.BR attributes (7),
.BR standards (7),
.BR threads (7)
.SH NOTES
The semantics of the \fBuc_stack\fR member of the \fBucontext_t\fR structure
have changed as they apply to inputs to \fBmakecontext()\fR. Prior to Solaris
10, the \fBss_sp\fR member of the \fBuc_stack\fR structure represented the high
memory address of the area reserved for the stack. The \fBss_sp\fR member now
represents the base (low memory address), in keeping with other uses of
\fBss_sp\fR.
.sp
.LP
This change in the meaning of \fBss_sp\fR is now the default behavior. The
\fB-D__MAKECONTEXT_V2_SOURCE\fR compilation flag used in Solaris 9 update
releases to access this behavior is obsolete.
.sp
.LP
Binary compatibility has been preserved with releases prior to Solaris 10.
Before recompiling, applications that use \fBmakecontext()\fR must be updated
to reflect this behavior change. The example below demonstrates a typical change
that must be applied:
.sp
.in +2
.nf
--- example1_s9.c       Thu Oct  3 11:58:17 2002
+++ example1.c  Thu Jun 27 13:28:16 2002
@@ -27,12 +27,9 @@
        uc.uc_stack.ss_sp = mmap(0, sz,
            PROT_READ | PROT_WRITE | PROT_EXEC,
            MAP_PRIVATE | MAP_ANON, -1, 0);
-       uc.uc_stack.ss_sp = (char *)uc.uc_stack.ss_sp + sz - 8;
        uc.uc_stack.ss_size = sz;
        uc.uc_stack.ss_flags = 0;

        uc.uc_link = &back

        makecontext(&uc, assign, 2, 100L, &value);
.fi
.in -2

